class Solution {
public:
    int dp[205][205];
    int ans = 0;
    int maximalRectangle(vector<vector<char>>& matrix) {
        for (int i = 0; i < matrix[0].size(); i++)
        {
            dp[0][i] = matrix[0][i] == '1' ? 1 : 0;
        }
        for (int i = 1; i < matrix.size(); i++)
        {
            for (int j = 0; j < matrix[i].size(); j++)
            {
                if (matrix[i][j] == '0') dp[i][j] = 0;
                else dp[i][j] = dp[i - 1][j] + 1;
            }
        }
        for (int i = 0; i < matrix.size(); i++)
        {
            stack<pair<int, int>> st;
            for (int j = 0; j < matrix[i].size(); j++)
            {
                int num = 0;
                while (!st.empty() && st.top().first > dp[i][j])
                {
                    ans = max(ans, st.top().first * (st.top().second + num));
                    num += st.top().second;
                    st.pop();
                }
                st.push(pair<int, int>(dp[i][j], 1 + num));
            }
            int num = 0;
            while (!st.empty())
            {
                ans = max(ans, st.top().first * (st.top().second + num));
                num += st.top().second;
                st.pop();
            }
        }
        return ans;
    }
};